//
//  ReviewView.swift
//  willdo
//
//  Created by zz on 3/14/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit
import SnapKit

class MyReviewView: UIView, UITableViewDataSource, UITableViewDelegate {

    let tableView = UITableView(frame: CGRect.zero, style: .grouped)
    
    var delegate:MyReviewViewDelegate?
    var modles:[MyReviewModle]?
    
    init(frame: CGRect, modles:[MyReviewModle]) {
        super.init(frame: frame)
        self.backgroundColor = BACKGROUND_GRAY
        tableView.tableHeaderView = UIView.init()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        self.modles = modles
        tableView.clipsToBounds = true
        addSubview(tableView)
        let ui = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 10))
        ui.backgroundColor = UIColor.clear
        tableView.tableHeaderView = ui
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
    }
    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "Reviewcell") as? MyReviewCell
        if cell == nil{
            cell = MyReviewCell()
        }
        var backgroundImg = "yellow"
        var fontColor = "#000000"
        switch modles![indexPath.section].state {
        case .needReceive: backgroundImg = "blue"; fontColor = "#2683f5"
        case .needCommit: backgroundImg = "red"; fontColor = "#d24d4e"
        case .needReview: backgroundImg = "yellow"; fontColor = "#f8b551"
        case .needArchive: backgroundImg = "green"; fontColor = "#61b87f"
        }
        cell?.ReviewState.textColor = UIColor(hexString: fontColor)
        cell?.bgView.image = UIImage(named: backgroundImg)
        cell?.setText(modle: modles![indexPath.section])
        return cell!
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let ui = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 6))
        ui.backgroundColor = UIColor.red
        return ui
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 5
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//        let ui = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 6))
//        ui.backgroundColor = UIColor.green
//        return ui
        return nil
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegate?.myReviewCellDidSelect(index: indexPath)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return (modles!.count)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

protocol MyReviewViewDelegate {
    func myReviewCellDidSelect(index: IndexPath)
}
